#include "mode_scene.h"

static LIGHT_EFFECT_T light_effect_dazzling_temporary_config;

void read_mode_handle()  //暖光
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into read_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
    }

    u8 buf[300];
    bzero(buf,300);

    for (int j = 0; j <= get_light_length(); j += 1)  //算出每个灯珠的颜色
    { 
        buf[j*3+0] = 0xFF; //R
        buf[j*3+1] = 0X39; //G
        buf[j*3+2] = 0X12; //B
    }
    logic_send_light_msg(buf);
}

void work_mode_handle() //白光
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into read_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
    }

    u8 buf[300];
    bzero(buf,300);
    for (int j = 0; j <= get_light_length(); j += 1)  //算出每个灯珠的颜色
    { 
        buf[j*3+0] = 250; //R
        buf[j*3+1] = 250; //G
        buf[j*3+2] = 200; //B
    }
    logic_send_light_msg(buf);

}

void sleep_mode_handle() //暗光
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into read_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
    }

    u8 buf[300];
    bzero(buf,300);
    for (int j = 0; j <= get_light_length(); j += 1)  //算出每个灯珠的颜色
    { 
        buf[j*3+0] = 20; //R
        buf[j*3+1] = 20; //G
        buf[j*3+2] = 20; //B
    }
    logic_send_light_msg(buf);
}


void aurora_mode_handle()  //极光
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[0],sizeof(LIGHT_EFFECT_T));
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }

    u8 buf[300];
    bzero(buf,300);
    get_cache_data(buf);
    logic_send_light_msg(buf);
}

void candlelight_mode_handle()  //烛光
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[1],sizeof(LIGHT_EFFECT_T));
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }

    u8 buf[300];
    bzero(buf,300);
    get_cache_data(buf);
    logic_send_light_msg(buf);

}

void birthday_mode_handle()  //生日
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[2],sizeof(LIGHT_EFFECT_T));
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }

    u8 buf[300];
    bzero(buf,300);
    get_cache_data(buf);
    logic_send_light_msg(buf);
}

void meteor_mode_handle()  //休息
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[3],sizeof(LIGHT_EFFECT_T));
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }

    u8 buf[300];
    bzero(buf,300);
    get_cache_data(buf);
    logic_send_light_msg(buf);
}

void rest_mode_handle()  //休息
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[4],sizeof(LIGHT_EFFECT_T));
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }

    u8 buf[300];
    bzero(buf,300);
    get_cache_data(buf);
    logic_send_light_msg(buf);
}



void meditation_mode_handle()  //冥想
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[5],sizeof(LIGHT_EFFECT_T));
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }

    u8 buf[300];
    bzero(buf,300);
    get_cache_data(buf);
    logic_send_light_msg(buf);
}

void flame_mode_handle()  //火焰
{
    if (get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[6],sizeof(LIGHT_EFFECT_T));
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }

    u8 buf[300];
    bzero(buf,300);
    get_cache_data(buf);
    logic_send_light_msg(buf);
}


void business_mode_handle()  //商务
{
    if(get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[7],sizeof(LIGHT_EFFECT_T));
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }

    u8 buf[300];
    bzero(buf,300);
    get_cache_data(buf);
    logic_send_light_msg(buf);
}

void alarm_mode_handle()  //警报
{
    if(get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[8],sizeof(LIGHT_EFFECT_T));
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    u8 buf[300];
    bzero(buf,300);
    get_cache_data(buf);
    logic_send_light_msg(buf);
}

void tide_mode_handle()  //潮汐
{
    if(get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[9],sizeof(LIGHT_EFFECT_T));
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }

    u8 buf[300];
    bzero(buf,300);
    get_cache_data(buf);
    logic_send_light_msg(buf);
}

void music_mode_handle()  //电子音乐
{
    if(get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[10],sizeof(LIGHT_EFFECT_T));
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    u8 buf[300];
    bzero(buf,300);
    get_cache_data(buf);
    logic_send_light_msg(buf);
}



void universe_mode_handle()  //宇宙
{
    if(get_scene_last_ssp() != get_scene_ssp() )
    {
        LOG_INFO("into aurora_mode_handle()\n");
        set_scene_last_ssp(get_scene_ssp());
        memcpy(&light_effect_dazzling_temporary_config,&light_effect_default_config[11],sizeof(LIGHT_EFFECT_T));
        set_light_effect_config(light_effect_dazzling_temporary_config.light_effect_ssp,light_effect_dazzling_temporary_config.wait,\
        light_effect_dazzling_temporary_config.len,light_effect_dazzling_temporary_config.buf);
    }
    u8 buf[300];
    bzero(buf,300);
    get_cache_data(buf);
    logic_send_light_msg(buf);
}

SCENE_HANDLE scene_mode_dazzling_handle[SCENE_MODE_NUM] =
{
    {},
    {read_mode_handle}, //阅读模式
    {work_mode_handle}, //工作模式
    {sleep_mode_handle}, //睡眠模式
    {aurora_mode_handle}, //极光模式
    {candlelight_mode_handle}, //烛光
    {birthday_mode_handle},//生日
    {meteor_mode_handle}, //流星
    {rest_mode_handle},//休息
    {meditation_mode_handle},//冥想
    {flame_mode_handle},//火焰
    {business_mode_handle},//商务
    {alarm_mode_handle},//警报
    {tide_mode_handle},//潮汐
    {music_mode_handle},//电子音乐
    {universe_mode_handle},//宇宙
};


void scene_mode()
{
    if(scene_mode_dazzling_handle[get_scene_ssp()].handle)
    {
        scene_mode_dazzling_handle[get_scene_ssp()].handle();
    }
}

void mode_scene(SYS_OPT_KEY opt)
{
    static int time;
    if (app_logic_get_last_ssp() != app_logic_get_ssp()) //第一次进入
    {
        LOG_INFO("mode_scene_dazzling_11\n");
        set_custom_recssp(0); //强制复位
        app_logic_set_last_ssp(app_logic_get_ssp());
        time = arch_os_ms_now();
    }

    if(arch_os_ms_elapsed(time)>=(10)) //ms轮询一次
    {
        scene_mode();
        time = arch_os_ms_now();
    }

    if(opt.oepation == OPT_POWER_TURNOFF) //关机
    {
        app_logic_set_ssp(SSP_POWER_OFF);
    }

}